﻿using DBOptionPro.Model;
using DevExpress.Mvvm;
using DevExpress.Utils.Drawing.Helpers;
using DevExpress.XtraEditors.Controls;
using DevUIUtils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBOptionPro
{
    /// <summary>
    /// 主视图
    /// </summary>
    public partial class MainView : DevExpress.XtraEditors.XtraForm
    {
        public MainView()
        {
            InitializeComponent();
            if (!mvvmContext.IsDesignMode)
            {
                InitUI();
                InitService();
                InitBindings();
            }
        }

        void InitUI()
        {
            this.gridView1.SetSNInRowHead(true, 50);
            this.gridView1.LoadStyle_EditView();
            this.btnDir.SelectDirectory(string.Empty);

            this.gridView2.SetSNInRowHead(true, 50);
            this.gridView2.LoadStyle_EditView();
            this.btnDBFDir.SelectDirectory(string.Empty);
        }

        void InitService()
        {
            Messenger.Default.Register<string>(this, msg =>
            {
                this.txtInfo.DisplayMsg(msg, Color.Black);
            });
        }

        void InitBindings()
        {
            var fluent = mvvmContext.OfType<MainViewModel>();
            fluent.SetBinding(this, v => v.Text, vm => vm.AppTitle);
            fluent.SetBinding(this.lblAboutTitle, v => v.Text, vm => vm.AppTitle, "软件名称：{0}");
            #region 文件搜索 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            fluent.SetBinding(this.btnDir, v => v.EditValue, vm => vm.SearchDir);
            fluent.SetBinding(this.gridControl1, v => v.DataSource, vm => vm.Files);
            fluent.SetItemsSourceBinding(
                this.cboAreaSelect,
                v => v.Items, vm => vm.Areas,
                (vi, area) => vi.Value.Equals(area.AreaCode),
                area => new ImageComboBoxItem(description: area.AreaName, value: area.AreaCode),
                null, null);

            fluent.BindCommand(this.btnSearch, vm => vm.SearchFiles);
            fluent.BindCommand(this.btnOption.Buttons[2], vm => vm.RemoveDBFileInfo, p => this.gridView1.GetFocusedRow() as DBFileInfo);
            fluent.BindCommand(this.btnOption.Buttons[0], vm => vm.BJDBFileInfo, p => this.gridView1.GetFocusedRow() as DBFileInfo);
            fluent.BindCommand(this.btnOption.Buttons[1], vm => vm.RBJDBFileInfo, p => this.gridView1.GetFocusedRow() as DBFileInfo);

            fluent.BindCommand(this.btnSave, vm => vm.SaveFiles());
            #endregion 文件搜索 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            #region 系统设置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            fluent.SetObjectDataSourceBinding(this.BS_Option, vm => vm.Option);

            fluent.BindCommand(this.btnTestLink, vm => vm.TestLink);
            fluent.BindCommand(this.btnSaveOption, vm => vm.SaveOption);
            #endregion 系统设置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            #region 数据同步 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            fluent.BindCommand(this.btnStartSync, vm => vm.StartSync);
            fluent.BindCommand(this.btnStopSync, vm => vm.StopSync);
            #endregion 数据同步 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

            #region DBF监控 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            fluent.SetBinding(this.btnDBFDir, v => v.EditValue, vm => vm.Option.DBFMontiorParams.FileDirectory);
            fluent.SetBinding(this.txtDBFFile1Name, v => v.EditValue, vm => vm.Option.DBFMontiorParams.FileNames);
            fluent.SetBinding(this.gridControl2, v => v.DataSource, vm => vm.DBFFiles);
            fluent.BindCommand(this.btnMonitorDBF, vm => vm.MonitorDBF);
            fluent.BindCommand(this.btnStopMonitorDBF, vm => vm.StopMonitorDBF);
            fluent.BindCommand(this.btnDBFEdit.Buttons[0], vm => vm.RemoveDBFFile, p => this.gridView2.GetFocusedRow() as DBFFileInfo);
            #endregion DBF监控 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        }
    }
}
